package com.fowo.api.syncAmazon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fowo.api.entity.*;
import com.fowo.api.mapper.*;
import com.fowo.api.model.product.ProductVo;
import com.fowo.api.service.impl.AmazonFbmRefundOrdersServiceImpl;
import com.fowo.api.syncAmazon.api.FbaRefundOrdersReportService;
import com.fowo.api.syncAmazon.api.FbmRefundOrdersReportService;
import com.fowo.api.syncAmazon.mapper.AmazonTmpMapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class RefundOrdersReportServiceImpl extends ServiceImpl<AmazonFbaRefundOrdersMapper, AmazonFbaRefundOrders> {

    @Resource
    private TmpFbaRefundOrdersMapper refundOrdersMapper;
    @Resource
    private ListingFatherMapper listingFatherMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private FbaRefundOrdersReportService reportService;
    @Resource
    private FbmRefundOrdersReportService fbmRefundOrdersReportService;
    @Resource
    private SysCountryMapper sysCountryMapper;
    @Resource
    private TmpFbmRefundOrdersMapper fbmRefundOrdersMapper;
    @Resource
    private AmazonFbmRefundOrdersServiceImpl fbmRefundOrdersService;
    @Resource
    private AmazonTmpMapper amazonTmpMapper;

    /**
     * 同步fba退货订单
     * @throws Exception
     */
    public void syncRefundOrders() throws Exception {
        //清空临时表
        amazonTmpMapper.deleteAllFBARefundOrder();
        //  拉数据到临时表
        reportService.getFbaRefundOrders();

        //获取临时表数据
        List<TmpFbaRefundOrders> tmpRefundOrders = refundOrdersMapper.selectList(new QueryWrapper<TmpFbaRefundOrders>().eq("del_flag", 0));
        List<AmazonFbaRefundOrders> insertList = new ArrayList<>();
        if(tmpRefundOrders != null && tmpRefundOrders.size() > 0){
            for (TmpFbaRefundOrders orders:tmpRefundOrders) {
                //重复校验
                QueryWrapper<AmazonFbaRefundOrders> orderWrapper = new QueryWrapper<>();
                orderWrapper.eq("order_id",orders.getOrderId());
                orderWrapper.eq("del_flag",0);
                int count = (int)this.count(orderWrapper);
                if(count > 0){
                    continue;
                }
                AmazonFbaRefundOrders amazonFbaRefundOrders = new AmazonFbaRefundOrders();
                BeanUtils.copyProperties(orders, amazonFbaRefundOrders);
                amazonFbaRefundOrders.setCreateTime(new Date());
                insertList.add(amazonFbaRefundOrders);
            }
            List<List<AmazonFbaRefundOrders>> partition = Lists.partition(insertList, 200);
            for (List<AmazonFbaRefundOrders> amazonFbaRefundOrdersList:partition) {
                this.saveBatch(amazonFbaRefundOrdersList);
            }
        }
    }

    /**
     * 同步fbm退货订单
     * @throws Exception
     */
    public void syncFbmRefundOrders() throws Exception {
        fbmRefundOrdersReportService.getFbmRefundOrders();
        reportService.getFbaRefundOrders();
        //获取listing数据
        QueryWrapper<ListingFather> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flag", 0);
        wrapper.isNotNull("shop");
        wrapper.isNotNull("sku");
        wrapper.isNotNull("asin");
        List<ListingFather> listingFathers = listingFatherMapper.selectList(wrapper);
        Map<String, Map<Long, Map<String, List<ListingFather>>>> collect = listingFathers.stream().collect(Collectors.groupingBy(ListingFather::getMsku,
                Collectors.groupingBy(ListingFather::getShop, Collectors.groupingBy(ListingFather::getAsin))));

        //获取临时表数据
        List<TmpFbmRefundOrders> tmpRefundOrders = fbmRefundOrdersMapper.selectList(new QueryWrapper<TmpFbmRefundOrders>().eq("del_flag", 0));
        List<AmazonFbmRefundOrders> insertList = new ArrayList<>();
        if(tmpRefundOrders != null && tmpRefundOrders.size() > 0){
            for (TmpFbmRefundOrders orders:tmpRefundOrders) {
                QueryWrapper<AmazonFbmRefundOrders> fbmWrapper = new QueryWrapper<>();
                fbmWrapper.eq("del_flag",0);
                fbmWrapper.eq("order_id",orders.getOrderId());
                int count = (int)fbmRefundOrdersService.count(fbmWrapper);
                if(count > 0){
                    continue;
                }
                AmazonFbmRefundOrders fbmRefundOrders = new AmazonFbmRefundOrders();
                fbmRefundOrders.setCreateTime(new Date());
                BeanUtils.copyProperties(orders, fbmRefundOrders);
                //插入其他数据
                Map<Long, Map<String, List<ListingFather>>> longMapMap = collect.get(orders.getMerchantSku());
                if(longMapMap != null){
                    Map<String, List<ListingFather>> stringListMap = longMapMap.get(orders.getSid());
                    if(stringListMap != null && stringListMap.size() > 0){
                        List<ListingFather> listingFathers1 = stringListMap.get(orders.getAsin());
                        if(listingFathers1 != null && listingFathers1.size() > 0){
                            ListingFather father = listingFathers1.get(0);
                            QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
                            queryWrapper.eq("del_flag",0);
                            queryWrapper.eq("sku",father.getSku());
                            Product product = productMapper.selectOne(queryWrapper);
                            if(product != null){
                                fbmRefundOrders.setName(product.getName());
                            }
                            fbmRefundOrders.setSku(father.getSku());
                        }
                    }
                }
                //发货仓库编号
                insertList.add(fbmRefundOrders);
            }
            List<List<AmazonFbmRefundOrders>> partition = Lists.partition(insertList, 200);
            for (List<AmazonFbmRefundOrders> fbmRefundOrders:partition) {
                fbmRefundOrdersService.saveBatch(fbmRefundOrders);
            }
            List<Long> collect1 = tmpRefundOrders.stream().map(TmpFbmRefundOrders::getId).collect(Collectors.toList());
            fbmRefundOrdersMapper.deleteBatchIds(collect1);
        }
    }

    private Map<String, List<SysCountry>> getCountry() {
        //获取国家数据
        QueryWrapper<SysCountry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag",0);
        queryWrapper.isNotNull("code");
        List<SysCountry> sysCountries = sysCountryMapper.selectList(queryWrapper);
        Map<String, List<SysCountry>> countries = null;
        if(sysCountries != null && sysCountries.size() > 0){
            countries = sysCountries.stream().collect(Collectors.groupingBy(SysCountry::getCountry));
        }
        return countries;
    }

}
